//
//  RJAQIBar.m
//  AirQuality
//
//  Created by Po on 15/4/2.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJAQIBar.h"
#import "RJBarTop.h"
#import "RJBarTopTouch.h"
#import "RJBar.h"
#import "RJBarBottom.h"
@interface RJAQIBar () <RJBarTopTouch>

@property (retain, nonatomic) RJBar * barView;
@property (retain, nonatomic) UIImageView * barBack;
@property (retain, nonatomic) NSMutableDictionary * data;
@property (retain, nonatomic) RJBarTop * barTop;
@property (retain, nonatomic) RJBarTopTouch * batTopTouch;
@property (retain, nonatomic) RJBarBottom * barBottom;

@property (assign, nonatomic) CGFloat topViewHeight;
@property (assign, nonatomic) BOOL isAnim;
@property (assign, nonatomic) CGRect trueFrame;
@property (assign, nonatomic) CGFloat barMargin;


- (void)initInterface;
@end

@implementation RJAQIBar

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data isAnim:(BOOL)isAnim
{
    self = [super initWithFrame:frame];
    if (self) {
        _isAnim = isAnim;
        _data = [NSMutableDictionary dictionaryWithDictionary:data];
        _topViewHeight = frame.size.width / 2;
        _barMargin = 6;
        if ([_data[@"title"] isEqualToString:@"CO"]) {
            _data[@"dataNum"]= [NSString stringWithFormat:@"%.2f",[_data[@"dataNum"] floatValue]];
        }
        
        [self initInterface];
    }
    return self;
}

- (void)initInterface
{
//    [self addSubview:[self getBarBack]];
    [self addSubview:[self getBarWithData:_data]];
    [self addSubview:[self getBarTop]];
    [self addSubview:[self getBarBottom]];
    
    //是否有动画
    if (_isAnim) {
        [UIView animateWithDuration:2 animations:^{
            [_barView setFrame:_trueFrame];
        }];
        
        CGRect rect = _barTop.frame;
        rect.origin.y = _trueFrame.origin.y - rect.size.height;
        [UIView animateWithDuration:2 animations:^{
            [_barTop setFrame:rect];
        }];
    }
    
}

- (RJBar *)getBarWithData:(NSDictionary *)data
{
    if (!_barView) {
        if (_isAnim) {
            
        }
        CGRect rect = self.bounds;
        rect.origin.x += 1;
        
        CGFloat tempDataNum = [data[@"dataNum"] floatValue];
        if (tempDataNum > 300) {
            tempDataNum = 300;
        }
        rect.size.height = (rect.size.height / 7 * 5) / 300 * tempDataNum;
        
        
        rect.origin.y = (self.bounds.size.height - (rect.size.height + self.bounds.size.height / 7));
        
//        rect.size.width -= 2;
        if (_isAnim) {
            _trueFrame = rect;
            rect.origin.y = rect.size.height + rect.origin.y;
            rect.size.height = 0;
        }
        _barView = [[RJBar alloc] initWithFrame:rect maxNum:[data[@"maxNum"] floatValue] dataNum:[data[@"dataNum"] floatValue] color:_data[@"color"]];
    }
    return _barView;
}

- (UIImageView *)getBarBack
{
    if (!_barBack) {
        CGRect rect = self.bounds;
        rect.origin.x += _barMargin;
        rect.origin.y += (_topViewHeight - 5);
        rect.size.height -= (rect.size.width + _topViewHeight);
        rect.size.width -= _barMargin * 2;
        _barBack = [[UIImageView alloc] initWithFrame:rect];
        [_barBack setImage:[UIImage imageNamed:@"barBack"]];
        [_barBack.layer setCornerRadius:rect.size.width / 2];
        _barBack.layer.masksToBounds = YES;
    }
    return _barBack;
}

- (RJBarTop *)getBarTop
{
    if (!_barTop) {
        CGFloat y = CGRectGetMaxY(_barView.frame);
        CGFloat width = self.frame.size.width;
        _barTop = [[RJBarTop alloc] initWithFrame:CGRectMake(0, y - _topViewHeight, width, _topViewHeight) color:_data[@"color"] num:GetStringWithNum(_data[@"dataNum"]) ];
    }
    return _barTop;
}

- (RJBarBottom *)getBarBottom
{
    if (!_barBottom) {
        CGRect rect = self.bounds;
        rect.origin.y =  rect.size.height / 7 * 6;
        rect.origin.x += 1;
        rect.size.height = rect.size.height / 7;
        rect.size.width -= 2;
        _barBottom = [[RJBarBottom alloc] initWithFrame:rect title:_data[@"title"]];
    }
    return _barBottom;
}

#pragma mark - event
- (void)touchStart
{
    [_barTop showHighlightShow];
}

- (void)touchEnd
{
    [_barTop reSetShow];
}

- (void)removeAllAnim
{
    [_barTop removeAllAnim];
}
@end
